<template>
  <Dialog v-model="dialogVisible" title="完成项目" width="900px">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="130px"
      v-loading="formLoading"
    >
      <el-row :gutter="20">
        <!-- <el-col :span="12">
          <el-form-item label="项目编号" prop="projectNumber">
            <el-input v-model="formData.projectNumber" placeholder="请输入项目编号" />
          </el-form-item>
          <el-form-item label="项目类型" prop="projectType">
            <el-checkbox-group v-model="formData.projectType">
              <el-checkbox
                v-for="dict in getIntDictOptions(DICT_TYPE.OA_PROJECT_TYPE)"
                :key="dict.value"
                :label="String(dict.value)"
              >
                {{ dict.label }}
              </el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item label="是否为简化企业" prop="isSimplifiedEnterprise">
            <el-radio-group v-model="formData.isSimplifiedEnterprise">
              <el-radio
                v-for="dict in getIntDictOptions(DICT_TYPE.OA_IS_OR_NO)"
                :key="dict.value"
                :label="dict.value"
              >
                {{ dict.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否为重点企业" prop="isKeyEnterprise">
            <el-radio-group v-model="formData.isKeyEnterprise">
              <el-radio
                v-for="dict in getIntDictOptions(DICT_TYPE.OA_IS_OR_NO)"
                :key="dict.value"
                :label="dict.value"
              >
                {{ dict.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否计算项目数" prop="isCountedProject">
            <el-radio-group v-model="formData.isCountedProject">
              <el-radio
                v-for="dict in getIntDictOptions(DICT_TYPE.OA_IS_OR_NO)"
                :key="dict.value"
                :label="dict.value"
              >
                {{ dict.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="纳税人属性" prop="taxpayerAttribute">
            <el-select v-model="formData.taxpayerAttribute" placeholder="请选择纳税人属性">
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.OA_TAXPAYER_ATTRIBUTE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="税收情况" prop="taxSituation">
            <el-input v-model="formData.taxSituation" placeholder="请输入税收情况" />
          </el-form-item>
          <el-form-item label="招商负责人" prop="investmentPromotionOfficer">
            <el-input v-model="formData.investmentPromotionOfficer" placeholder="请输入招商负责人" />
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
          <el-form-item label="公司名称" prop="companyName">
            <el-input v-model="formData.companyName" placeholder="请输入公司名称" />
          </el-form-item>
          <!-- <el-form-item label="法人姓名" prop="legalPerson">
            <el-input v-model="formData.legalPerson" placeholder="请输入法人姓名" />
          </el-form-item>
          <el-form-item label="法人电话号码" prop="legalPersonPhone">
            <el-input v-model="formData.legalPersonPhone" placeholder="请输入法人实名电话号码" />
          </el-form-item>
          <el-form-item label="企业联系人" prop="enterpriseContact">
            <el-input v-model="formData.enterpriseContact" placeholder="请输入企业联系人" />
          </el-form-item>
          <el-form-item label="联系人电话" prop="enterpriseContactPhone">
            <el-input v-model="formData.enterpriseContactPhone" placeholder="请输入企业联系人电话" />
          </el-form-item>
          <el-form-item label="注册园区及平台" prop="registeredParkPlatform">
            <el-input v-model="formData.registeredParkPlatform" placeholder="请输入注册园区及平台" />
          </el-form-item>
          <el-form-item label="收费金额" prop="serviceFee">
            <el-input v-model="formData.serviceFee" placeholder="请输入收费金额" />
          </el-form-item>
          <el-form-item label="介绍人或下级平台" prop="introducerOrSubPlatform">
            <el-input v-model="formData.introducerOrSubPlatform" placeholder="请输入介绍人或下级平台" />
          </el-form-item>
          <el-form-item label="项目引进情况说明" prop="remark">
            <el-input v-model="formData.remark" type="textarea" placeholder="请输入项目引进情况说明" />
          </el-form-item> -->
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submitForm" :loading="formLoading">确 定</el-button>
    </template>
  </Dialog>
</template>

<script lang="ts" setup>
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { ProjectManagementApi } from '@/api/oa/projectmanagement'
import type {  ProjectManagementVO } from '@/api/oa/projectmanagement'

// 必须定义组件名
defineOptions({ name: 'CompleteProjectDialog' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const formLoading = ref(false) // 表单的加载中
const formData = ref<Partial<ProjectManagementVO>>({})
const formRules = reactive({
  projectNumber: [{ required: true, message: '项目编号不能为空', trigger: 'blur' }],
  companyName: [{ required: true, message: '公司名称不能为空', trigger: 'blur' }],
  // 可根据实际需要补充更多校验规则
})
const formRef = ref() // 表单 Ref

/** 打开弹窗，回显数据 */
const open = (row: ProjectManagementVO) => {
  dialogVisible.value = true
  // 回显所有字段，类型转换处理
  formData.value = {
    ...row,
    projectType: row.projectType
      ? String(row.projectType).split(',').filter(Boolean).map(v => String(v))
      : [],
    serviceFee: row.serviceFee ?? undefined,
    remark: row.remark ?? '',
    // 其他字段按需补充
  }
}
defineExpose({ open }) // 提供 open 方法

/** 提交表单，调用 update 接口 */
const emit = defineEmits(['success'])
const submitForm = async () => {
  if (!formRef.value) return
  await formRef.value.validate()
  formLoading.value = true
  try {
    const data: ProjectManagementVO = {
      ...formData.value,
      // 项目类型转字符串
      projectType: Array.isArray(formData.value.projectType)
        ? (formData.value.projectType as string[]).join(',')
        : formData.value.projectType,
      projectStatus: 2, // 办理完成
    } as ProjectManagementVO
    await ProjectManagementApi.updateProjectManagement(data)
    message.success('项目办理完成')
    dialogVisible.value = false
    emit('success')
  } finally {
    formLoading.value = false
  }
}
</script> 